﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication3
{
    public partial class SearcJobCode : Form
    {
      //  public SearchTransaction searchtransaction = null;
          private String connString = Program.ConnStr;

      //  JobCodeDataBean jobcodedatabean = null;
        JobCodeTransaction jobcodetransaction = null;
         driverpaymentbean  driverpaymentbean = null;
        DriverPaymentTransaction paymenttransaction= null;
       // BillDatabean billdatabean = null;
       // BillTransaction billtransaction = null;

        public SearcJobCode()
        {
            InitializeComponent();
            jobcodetransaction = new JobCodeTransaction();
            paymenttransaction = new DriverPaymentTransaction();
            
        }

        private void SearcJobCode_Load(object sender, EventArgs e)
        {
            showalljobcode();

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {

            showjobcodebySearch();



        }








        /// <summary>
        /// Displays all job Code
        /// </summary>
        public void showalljobcode()
        {
            try
            {



                String query = "Select * from jobcodemastertable ";
                OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
                 
            DataSet ds = new DataSet();
                dAdapter.Fill(ds, "tbluserdataview");
                tbljobcodeview.DataSource = ds.Tables["tbluserdataview"].DefaultView;
               
           }
            catch (Exception)
            {
                MessageBox.Show("The application had met with some errors please restart  the application :\n error:closer MSAccess files");

            }


        }

       





        /// <summary>
        /// 
        /// search job code using CompanyName
        /// </summary>
        public void showjobcodebySearch()
        {
            try
            {


                String query = "Select * from jobcodemastertable where (companyName ='" + txtSearch.Text + "')";
                OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);


                DataSet ds = new DataSet();
                dAdapter.Fill(ds, "tbluserdataview");
                tbljobcodeview.DataSource = ds.Tables["tbluserdataview"].DefaultView;
            }
            catch (Exception exp)
            {
                MessageBox.Show("The application had met with some errors please restart  the application :\n error:close MSAccess files");
                Console.WriteLine(exp);
            }


        }
       

        /// <summary>
        /// Cancel button Clicked

        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btncancel_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
            dtpfromdate.Value = DateTime.Today;
            dtptodate.Value = DateTime.Today;
            showalljobcode();


        }

        

        /// <summary>
        /// search using to and from shipment date dates jobcode between dates
        /// also  if a company name is given it searchthat companies jobcode between these days
        /// </summary>
       
 
        private void dtpfromdate_ValueChanged(object sender, EventArgs e)
        {
            if (txtSearch.Text.Trim() == null || txtSearch.Text.Trim() == "")
            {



                try
                {
                    DateTime time = dtpfromdate.Value.Date;
                    DateTime time2 = dtptodate.Value.Date;

                    String query = "Select * from jobcodemastertable  where (shipmentdate between @shipmentdate1 and @shipmentdate2) ";
                    OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
                    dAdapter.SelectCommand.Parameters.AddWithValue("@shipmentdate1", time);
                    dAdapter.SelectCommand.Parameters.AddWithValue("@shipmentdate2", time2);
                    DataSet ds = new DataSet();
                    dAdapter.Fill(ds, "tbljobmastertable");
                    tbljobcodeview.DataSource = ds.Tables["tbljobmastertable"].DefaultView;
                }
                catch (Exception eps)
                {

                    MessageBox.Show("" + eps);
                }


            }
            else
            {
                try
                {
                    DateTime time = dtptodate.Value.Date;
                    DateTime time2 = dtptodate.Value.Date;
                    MessageBox.Show("" + time);
                    String query = "Select * from jobcodemastertable where (shipmentdate between @shipmentdate1 and @shipmentdate2) and jobcode='" + txtSearch.Text + "'";
                    OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
                    dAdapter.SelectCommand.Parameters.AddWithValue("@shipmentdate1", time);
                    dAdapter.SelectCommand.Parameters.AddWithValue("@shipmentdate2", time2);
                    DataSet ds = new DataSet();
                    dAdapter.Fill(ds, "tbljobmastertable");
                    tbljobcodeview.DataSource = ds.Tables["tbljobmastertable"].DefaultView;
                }
                catch (Exception eps)
                {

                    MessageBox.Show("" + eps);
                }

            }

        }

        private void dtptodate_ValueChanged(object sender, EventArgs e)
        {
            if (txtSearch.Text.Trim() == null || txtSearch.Text.Trim() == "")
            {



                try
                {
                    DateTime time = dtpfromdate.Value.Date;
                    DateTime time2 = dtptodate.Value.Date;

                    String query = "Select * from jobcodemastertable  where (shipmentdate between @shipmentdate1 and @shipmentdate2) ";
                    OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
                    dAdapter.SelectCommand.Parameters.AddWithValue("@shipmentdate1", time);
                    dAdapter.SelectCommand.Parameters.AddWithValue("@shipmentdate2", time2);
                    DataSet ds = new DataSet();
                    dAdapter.Fill(ds, "tbljobmastertable");
                    tbljobcodeview.DataSource = ds.Tables["tbljobmastertable"].DefaultView;
                }
                catch (Exception eps)
                {

                    MessageBox.Show("" + eps);
                }


            }
            else
            {
                try
                {
                    DateTime time = dtptodate.Value.Date;
                    DateTime time2 = dtptodate.Value.Date;
                    MessageBox.Show("" + time);
                    String query = "Select * from jobcodemastertable where (shipmentdate between @shipmentdate1 and @shipmentdate2) and jobcode='" + txtSearch.Text + "'";
                    OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
                    dAdapter.SelectCommand.Parameters.AddWithValue("@shipmentdate1", time);
                    dAdapter.SelectCommand.Parameters.AddWithValue("@shipmentdate2", time2);
                    DataSet ds = new DataSet();
                    dAdapter.Fill(ds, "tbljobmastertable");
                    tbljobcodeview.DataSource = ds.Tables["tbljobmastertable"].DefaultView;
                }
                catch (Exception eps)
                {

                    MessageBox.Show("" + eps);
                }

            }

        }

        private void driverPaymentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowno = tbljobcodeview.CurrentCellAddress.Y;
            if (rowno < 0)
            {
                lblstatus.Text = "Select A Row first";

            }
            else
            {

                String jobcode = tbljobcodeview.Rows[rowno].Cells[1].Value.ToString();
               
                 driverpaymentbean = paymenttransaction.getpaymentbyjobcode(jobcode);
                if ( driverpaymentbean != null)
                {
                    
                    DriverPaymentForm driverpaymenntForm = new DriverPaymentForm( driverpaymentbean);
                    this.Close();
                    driverpaymenntForm.Show();
                }
                else
                {
                    DriverPaymentForm driverpaymenntForm = new DriverPaymentForm(jobcode);
                    this.Close();
                    driverpaymenntForm.Show();
                   
                }

            }
        }



      















    
    }

}
